//
//  TKYCustomCharts.m
//  StationHouseHelper
//
//  Created by apple on 2018/7/11.
//  Copyright © 2018年 铁科院. All rights reserved.
//

#import "TKYCustomCharts.h"
#import "TKYCustomChartsCell.h"
@interface TKYCustomCharts()
@property (nonatomic, strong)NSArray *customChartsArr;

@end
@implementation TKYCustomCharts

- (instancetype)initWithFrame:(CGRect)frame withCustomChartsNSArray:(NSArray *)array {
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.collectionView];
        //柱状图数据
        self.customChartsArr = array;
        //默认选择第一行
        [self.collectionView selectItemAtIndexPath: [NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UICollectionViewScrollPositionNone];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.collectionView];
        //柱状图数据
        //默认选择第一行
        [self.collectionView selectItemAtIndexPath: [NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UICollectionViewScrollPositionNone];
    }
    return self;
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.customChartsArr.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    TKYCustomChartsCell *cell= [collectionView dequeueReusableCellWithReuseIdentifier:@"TKYCustomChartsCell" forIndexPath:indexPath];
    if (indexPath.item == 0) {
        [cell redBorderWidth];
    }else{
        [cell removRedBorderWidth];
    }
    cell.questionTypeListModel = self.customChartsArr[indexPath.row];
   // [self collectionView:self.collectionView didSelectItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    return cell;
}

#pragma mark--懒加载
- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
        layout.itemSize = CGSizeMake(kWidthScale *60, self.kx_height);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.minimumLineSpacing = 0;
        UICollectionView *collectionView;
        
        collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, self.kx_height) collectionViewLayout:layout];
        collectionView.delegate = self;
        collectionView.dataSource = self;
        collectionView.backgroundColor = [UIColor whiteColor];
        collectionView.bounces = YES;
        //注册cell
        [collectionView registerClass:[TKYCustomChartsCell class] forCellWithReuseIdentifier:@"TKYCustomChartsCell"];
        collectionView.scrollsToTop = NO;
        collectionView.showsVerticalScrollIndicator = NO;
        collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView = collectionView;
    }
    return _collectionView;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    //当前选择的
    TKYCustomChartsCell *cell2  = (TKYCustomChartsCell *)[collectionView cellForItemAtIndexPath:indexPath];
    if (indexPath.row != 0) {
        [self collectionView:collectionView didDeselectItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    }
    [cell2 redBorderWidth];
    if (self.delegate && [self.delegate respondsToSelector:@selector(TKY_customChartsDidSelectItemAtIndexPath:)])
    {
        // 调用代理方法
        [self.delegate TKY_customChartsDidSelectItemAtIndexPath:indexPath];
    }
}

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
    TKYCustomChartsCell *cell = (TKYCustomChartsCell *)[collectionView cellForItemAtIndexPath:indexPath];
    if (indexPath.row == 0) {
        [cell removRedBorderWidth];
    }
    [cell removRedBorderWidth];
    if (self.delegate && [self.delegate respondsToSelector:@selector(TKY_customChartsDidDeselectItemAtIndexPath:)])
    {
        // 调用代理方法
        [self.delegate TKY_customChartsDidDeselectItemAtIndexPath:indexPath];
    }
}

@end
